//
// Created by de on 3/24/21.
//

//#include <asio/packaged_task.hpp>
#include "future"
#include "TInitState.h"
#include "thread"
#include "chrono"

using namespace std;



TInitState::TInitState(StateSelection *p_StateSelection) {
    state = InitState;
    this->p_StateSelection = p_StateSelection;
    node = p_StateSelection->getRosHandler();
}

E_State TInitState::init() {

    bool ret =node->sendDeviceInit();

    return ret == true? Normal: Error;
}


E_State TInitState::prepare() {
    throw PlaformTException(__LINE__, __FILE__, "Mode Error"+std::to_string(state));
}

E_State TInitState::start() {
    throw PlaformTException(__LINE__,__FILE__, "Mode Error"+std::to_string(state));
}

E_State TInitState::reset() {
    throw PlaformTException(__LINE__,__FILE__, "Mode Error"+std::to_string(state));
}

E_State TInitState::stop() {
    throw PlaformTException(__LINE__,__FILE__, "Mode Error"+std::to_string(state));
}

StateStatus TInitState::getCurrentState() {
    return state;
}

E_State TInitState::workThread() {
    std::packaged_task<E_State(void)> pt1([]{
//        spdlog::info("init threading running ");
        std::this_thread::sleep_for(chrono::seconds(3));
        throw PlaformTException(__LINE__,__FILE__, "test");
        return Normal;
    });

    std::future<E_State> fu1 = pt1.get_future();
    std::thread t1(std::move(pt1) );
    t1.detach();

    E_State now = fu1.get();

    return  now;
}

